formProcessor = {
	ajax_callback: {'fname': '', 'field_name': 'ajax_callback'},
	processErrors: function(data){
		var errorTplField = '<ul class="errors"><li></li></ul>';
		for (var key in data) {
			$targetField = $('#'+key);
			if ( $targetField )
			{
				$targetFieldParent = $targetField.parent();
				if ( ! $( '.errors', $targetFieldParent ).length ){
					$tplObj = $(errorTplField);
					 var val = data [key];
					 var msgStr = '';
					 for (var key2 in val) {
						 msgStr += val[key2];
					 }
					 $('li', $tplObj).html(msgStr);
					 $targetFieldParent.append($tplObj);
				}
			}
		}
		$.colorbox.resize();
    },

   onLoad: function(){
      var that = formProcessor;
        $('form .save-btn').click(function(){
            var data = {};
            $form = $(this).closest('form');
            $('.input-field', $form).each(function(index, element){
                $element = $(element);
					if ($element.hasClass('ckeditor')) {
						$element.val( CKEDITOR.instances[$element.attr('name')].getData() );
					}
					 if ( $element.attr('name') == that.ajax_callback.field_name ) {
						 that.ajax_callback.fname = $element.val()
					 } else {
						data[$element.attr('name')] = $element.val();
					 }
            });
            var action = $form.attr('action');
            $.post(
              action,
              data,
              function(data) {
						if ( !data.success ){
							 formProcessor.processErrors( data );
						} else 
						{
							if ( that.ajax_callback.fname ){
								eval( that.ajax_callback.fname+"();" );
							}
							$.colorbox.close();
						}
              }
            );
            return false;
        });
    }
};